﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECP.Model;
using ECP.Service.Utilities;
using ECP.Utilities;
using ECP.Utilities.Controls;

namespace ECP.Client.Sequence
{
    public partial class SequenceAdminFrm : FormBase, IModelInfo
    {
        private DataTable DTSequence = new DataTable(BaseSequenceTable.TableName);
        private bool PermissionAccess;
        private bool PermissionAdd;
        private bool PermissionDelete;
        private bool PermissionEdit;
        private bool PermissionExport;

        public SequenceAdminFrm()
        {
            InitializeComponent();
        }

        private void SequenceAdminFrm_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.FormOnLoad();
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void grdSequenceView_DoubleClick(object sender, EventArgs e)
        {
            btnEdit.PerformClick();
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            foreach (DataRow row in this.DTSequence.Rows)
            {
                bool flag = true;
                if (row[BaseBusinessLogic.SelectedColumn].ToString() == flag.ToString())
                {
                    row[BaseBusinessLogic.SelectedColumn] = false.ToString();
                }
                else
                {
                    row[BaseBusinessLogic.SelectedColumn] = true.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.Add();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (BaseInterfaceLogic.CheckInputSelectAnyOne(this.DTSequence, BaseBusinessLogic.SelectedColumn) && (XtraMessageBox.Show("您确认重置吗？", AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK))
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    this.DoReSet();
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            base.ExportExcel(this.grdSequence, @"\Modules\Export\", "序列信息.csv");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.Delete();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataRow row in this.DTSequence.Rows)
            {
                row[BaseBusinessLogic.SelectedColumn] = true.ToString();
            }
        }

        private void SequenceAdminFrm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 0x74)
            {
                this.FormOnLoad();
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                searchData();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            searchData();
        }

        private bool CheckInputBatchDelete()
        {
            bool flag = false;
            int num = 0;
            BaseSequenceEntity entity = new BaseSequenceEntity();
            foreach (DataRowView view in this.DTSequence.DefaultView)
            {
                DataRow dataRow = view.Row;
                if (dataRow.RowState != DataRowState.Deleted)
                {
                    bool flag3 = true;
                    if (dataRow[BaseBusinessLogic.SelectedColumn].ToString() == flag3.ToString())
                    {
                        entity = new BaseSequenceEntity(dataRow);
                        if (!entity.FullName.Equals("ECP_SEQUENCE"))
                        {
                            num++;
                        }
                        else
                        {
                            XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0018, new string[] { "ECP_SEQUENCE" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            return flag;
                        }
                    }
                }
            }
            flag = num > 0;
            if (!flag)
            {
                XtraMessageBox.Show(AppMessage.MSG0024, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }

        private void DataTableAddColumn()
        {
            BaseInterfaceLogic.DataTableAddColumn(this.DTSequence, BaseBusinessLogic.SelectedColumn);
        }

        public int Delete()
        {
            if (this.CheckInputBatchDelete() && (XtraMessageBox.Show(AppMessage.MSG0015, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK))
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    this.BatchDelete();
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
            return 0;
        }

        private void DoReSet()
        {
            string[] selecteIDs = this.GetSelecteIDs();
            if (selecteIDs.Length > 0)
            {
                this.DTSequence = ServiceManager.Instance.SequenceService.Reset(base.UserInfo, selecteIDs);
                XtraMessageBox.Show(AppMessage.MSG0053, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.GetSequenceList();
            }
            else
            {
                XtraMessageBox.Show(AppMessage.MSG0023, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        public string CurrentEntityID
        {
            get
            {
                return BaseInterfaceLogic.GetDataGridViewEntityID(this.grdSequenceView, BaseSequenceTable.FieldID);
            }
        }

        public void Edit()
        {
            SequenceEditFrm edit = new SequenceEditFrm(this.CurrentEntityID);
            if (edit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                this.GetSequenceList();
                this.SetButtonState();
            }
        }

        private void FormOnLoad()
        {
            base.Localization(this);
            this.GetPermission();
            this.GetSequenceList();
        }

        public string Add()
        {
            string entityID = string.Empty;
            SequenceAddFrm add = new SequenceAddFrm();
            add.ShowDialog(this);
            if (add.Changed)
            {
                entityID = add.EntityID;
                this.GetSequenceList();
                this.SetButtonState();
            }
            return entityID;
        }

        private void BatchDelete()
        {
            this.DTSequence = ServiceManager.Instance.SequenceService.BatchDelete(base.UserInfo, this.GetSelecteIDs());
            this.GetSequenceList();
            this.SetButtonState();
        }

        private void GetPermission()
        {
            this.PermissionAccess = base.IsAuthorized("SequenceAdmin.Access");
            this.PermissionAdd = base.IsAuthorized("SequenceAdmin.Add");
            this.PermissionEdit = base.IsAuthorized("SequenceAdmin.Edit");
            this.PermissionDelete = base.IsAuthorized("SequenceAdmin.Delete");
            this.PermissionExport = base.IsAuthorized("SequenceAdmin.Export");
        }

        private string[] GetSelecteIDs()
        {
            return BaseInterfaceLogic.GetSelecteIDs(this.DTSequence, BaseSequenceTable.FieldID, BaseBusinessLogic.SelectedColumn);
        }

        private void GetSequenceList()
        {
            this.DTSequence = ServiceManager.Instance.SequenceService.GetList(base.UserInfo);
            this.DataTableAddColumn();
            this.grdSequence.DataSource = this.DTSequence.DefaultView;
            this.grdSequence.Refresh();
            this.SetButtonState();
        }

        private void SetButtonState()
        {
            this.btnSelectAll.Enabled = false;
            this.btnInvertSelect.Enabled = false;
            this.btnAdd.Enabled = false;
            this.btnEdit.Enabled = false;
            this.btnReset.Enabled = false;
            this.btnExport.Enabled = false;
            this.btnDelete.Enabled = false;
            if (this.DTSequence.DefaultView.Count == 0)
            {
                this.btnDelete.Enabled = false;
            }
            if (this.DTSequence.DefaultView.Count >= 1)
            {
                this.btnSelectAll.Enabled = (this.PermissionEdit || this.PermissionDelete) || this.PermissionExport;
                this.btnInvertSelect.Enabled = (this.PermissionEdit || this.PermissionDelete) || this.PermissionExport;
                this.btnAdd.Enabled = this.PermissionAdd;
                this.btnEdit.Enabled = this.PermissionEdit;
                this.btnReset.Enabled = this.PermissionEdit;
                this.btnExport.Enabled = this.PermissionExport;
                this.btnDelete.Enabled = this.PermissionDelete;
            }
            if (!this.PermissionDelete)
            {
                if (this.colCheckBox.ColumnEdit != null)
                {
                    this.colCheckBox.ColumnEdit.ReadOnly = !this.PermissionDelete;
                }
            }
        }

        private void searchData()
        {
            string text = this.txtSearch.Text;
            if (string.IsNullOrEmpty(text))
            {
                this.DTSequence.DefaultView.RowFilter = string.Empty;
            }
            else
            {
                text = BaseBusinessLogic.GetSearchString(text);
                this.DTSequence.DefaultView.RowFilter = BaseSequenceTable.FieldFullName + " LIKE '" + text + "' OR " + BaseSequenceTable.FieldPrefix + " LIKE '" + text + "' OR " + BaseSequenceTable.FieldDescription + " LIKE '" + text + "'";
            }
            this.SetButtonState();
        }

        private DisplayTypes _type = DisplayTypes.TabPage;

        public DisplayTypes DisplayType
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }

        public string ModelCode
        {
            get
            {
                return this.GetType().FullName;
            }
        }

        public string ModelName
        {
            get
            {
                return this.Text;
            }
        }

        public IList<PermissionInfo> Permissions
        {
            get
            {
                return new List<PermissionInfo>() {
            new PermissionInfo("SequenceAdmin.Access", "查看序列") ,
                new PermissionInfo("SequenceAdmin.Add", "添加序列") ,
                    new PermissionInfo("SequenceAdmin.Edit", "编辑序列") ,
                        new PermissionInfo("SequenceAdmin.Delete", "删除序列") ,
            new PermissionInfo("SequenceAdmin.Export", "导出序列")
        };
            }
        }

        public void StartModel()
        {
            this.ShowDialog();
        }
    }
}